<?php

namespace app\company\controller\config\software;

use app\BaseController;
use app\model\CompanyConfigModel;
use app\model\CompanyModel;
use app\model\ConfigModel;
use app\model\platform\PlatformConfigModel;
use app\model\PlatformModel;
use think\App;

class Index extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 软件设置-获取信息
     * @return bool|string
     */
    public function getPlatform(): bool|string
    {
        try {
            $PlatformConfigModel = new PlatformConfigModel();
            $data = $PlatformConfigModel->where([
                ['platform_id','=',$this->userData['platform_id']],
                ['key','=','software_settings']
            ])->value('value');
            $data = $data?json_decode($data,true):[];
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 软件设置-设置
     * @return bool|string
     */
    public function setPlatform(): bool|string
    {
        try {
            $param = $this->request->param();
            $PlatformConfigModel = new PlatformConfigModel();
            $info = $PlatformConfigModel->where([
                ['platform_id','=',$this->userData['platform_id']],
                ['key','=','software_settings']
            ])->find();
            if (!$info) throw new \Exception('参数错误！');
            $info->value = json_encode($param,true);
            $info->save();
            return success([],'更新成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 数据大屏设置-获取信息
     * @return bool|string
     */
    public function getPlatformLandscape(): bool|string
    {
        try {
            $PlatformConfigModel = new PlatformConfigModel();
            $data = $PlatformConfigModel->where([
                ['platform_id','=',$this->userData['platform_id']],
                ['key','=','landscape_settings']
            ])->value('value');
            $data = $data?json_decode($data,true):[];
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 数据大屏设置-设置
     * @return bool|string
     */
    public function setPlatformLandscape(): bool|string
    {
        try {
            $param = $this->request->param();
            $PlatformConfigModel = new PlatformConfigModel();
            $info = $PlatformConfigModel->where([
                ['platform_id','=',$this->userData['platform_id']],
                ['key','=','landscape_settings']
            ])->find();
            if (!$info) throw new \Exception('参数错误！');
            $info->value = json_encode($param,true);
            $info->save();
            return success([],'更新成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 获取平台信息
     * @return bool|string
     */
    public function getPlatformInfo(): bool|string
    {
        try {
            $PlatformModel = new PlatformModel();
            $info = $PlatformModel->find($this->userData['platform_id']);
            return success($info);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 更新平台信息
     * @return bool|string
     */
    public function setPlatformInfo(): bool|string
    {
        try {
            $param = $this->request->param();
            $PlatformModel = new PlatformModel();
            $info = $PlatformModel->find($this->userData['platform_id']);
            if (!$info) throw new \Exception('参数错误！');
            $info->save($param);
            return success([],'更新成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


    /**
     * 软件设置-获取信息-公司
     * @return bool|string
     */
    public function getCompany(): bool|string
    {
        try {
            $CompanyConfigModel = new CompanyConfigModel();
            $data = $CompanyConfigModel->where([
                ['company_id','=',$this->userData['company_id']],
                ['key','=','software_settings']
            ])->value('value');
            $data = $data?json_decode($data,true):[];
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 软件设置-设置-公司
     * @return bool|string
     */
    public function setCompany(): bool|string
    {
        try {
            $param = $this->request->param();
            $CompanyConfigModel = new CompanyConfigModel();
            $info = $CompanyConfigModel->where([
                ['company_id','=',$this->userData['company_id']],
                ['key','=','software_settings']
            ])->find();
            if (!$info) throw new \Exception('参数错误！');
            $info->value = json_encode($param,true);
            $info->save();
            return success([],'更新成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 数据大屏设置-获取信息-公司
     * @return bool|string
     */
    public function getCompanyLandscape(): bool|string
    {
        try {
            $CompanyConfigModel = new CompanyConfigModel();
            $data = $CompanyConfigModel->where([
                ['company_id','=',$this->userData['company_id']],
                ['key','=','landscape_settings']
            ])->value('value');
            $data = $data?json_decode($data,true):[];
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 数据大屏设置-设置-公司
     * @return bool|string
     */
    public function setCompanyLandscape(): bool|string
    {
        try {
            $param = $this->request->param();
            $CompanyConfigModel = new CompanyConfigModel();
            $info = $CompanyConfigModel->where([
                ['company_id','=',$this->userData['company_id']],
                ['key','=','landscape_settings']
            ])->find();
            if (!$info) throw new \Exception('参数错误！');
            $info->value = json_encode($param,true);
            $info->save();
            return success([],'更新成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 获取公司信息
     * @return bool|string
     */
    public function getCompanyInfo(): bool|string
    {
        try {
            $CompanyModel = new CompanyModel();
            $info = $CompanyModel->find($this->userData['company_id']);
            return success($info);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 更新公司信息
     * @return bool|string
     */
    public function setCompanyInfo(): bool|string
    {
        try {
            $param = $this->request->param();
            $CompanyModel = new CompanyModel();
            $info = $CompanyModel->find($this->userData['company_id']);
            if (!$info) throw new \Exception('参数错误！');
            $info->save($param);
            return success([],'更新成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 获取单位配置信息
     * @return bool|string
     */
    public function getUnitInfo(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'unit_id|单位ID' => 'require',
                'key|键' => 'require',
            ]);
            $ConfigModel = new ConfigModel();
            $info = $ConfigModel->where([
                ['unit_id','=',$param['unit_id']],
                ['key','=',$param['key']]
            ])->value('value');
            $info = $info?json_decode($info,true):[];
            return success($info);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

}